package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.entity.BusSales;
import com.example.warehouse.bus.pojo.param.BusSalesAddNewParam;
import com.example.warehouse.bus.pojo.param.BusSalesApprovalSalesParam;
import com.example.warehouse.bus.pojo.param.BusSalesSelectParam;
import com.example.warehouse.bus.pojo.param.BusSalesUpdateParam;
import com.example.warehouse.bus.service.IBusSalesService;
import com.example.warehouse.common.pojo.vo.PageBean;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.JsonResult;
import com.example.warehouse.core.excel.ExcelUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 销售订单_控制层
 * pipibee
 */
@Slf4j
@RequestMapping("/busSales")
@RestController
@Api(tags = "6.1. 销售订单_管理")
public class BusSalesController {

    @Autowired
    private IBusSalesService iBusSalesService;

    public BusSalesController() {
        log.info("创建控制器对象:BusSalesController");
    }

    @ApiOperation("添加_销售订单")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/busSalers/add-new')")
    @PostMapping("/add-new")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "顾客ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "number", value = "销售数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "description", value = "备注", required = true, dataType = "String"),
            @ApiImplicitParam(name = "salePrice", value = "售出价格", required = true, dataType = "BigDecimal"),
            @ApiImplicitParam(name = "payType", value = "支付类型", required = true, dataType = "String")
    })
    public JsonResult addNewBusSales(@RequestBody BusSalesAddNewParam busSalesAddNewParam, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【添加_销售订单】的请求，参数：{},{}", busSalesAddNewParam, currentPrincipal);
        iBusSalesService.addNewBusSales(busSalesAddNewParam, currentPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation("逻辑删除_销售订单")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/busSalers/delete')")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "销售订单ID", required = true, dataType = "Long"),
    })
    public JsonResult deleteBusSalesById(@RequestBody Long id, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【逻辑删除_销售订单(逻辑)】的请求，参数：{},{}", id, currentPrincipal);
        iBusSalesService.delete(id, currentPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation("任意项修改_销售订单")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/busSalers/update')")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "销售订单ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "customerId", value = "顾客ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "number", value = "销售数量", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "description", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(name = "salePrice", value = "售出价格", required = false, dataType = "BigDecimal"),
            @ApiImplicitParam(name = "payType", value = "支付类型", required = false, dataType = "String"),
            @ApiImplicitParam(name = "approval_status", value = "审批状态", required = false, dataType = "Integer"),
    })
    public JsonResult updateBusSalesById(@RequestBody BusSalesUpdateParam busSalesUpdateParam, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【任意项修改_销售订单】的请求，参数：{},{}", busSalesUpdateParam, currentPrincipal);
        iBusSalesService.update(busSalesUpdateParam, currentPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation("审批_销售订单")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/busSalers/update')")
    @PostMapping("/approval")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "销售订单ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "approval", value = "审批结果", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "goodsNum", value = "变化数量", required = true, dataType = "Integer")
    })
    public JsonResult approvalSales(@RequestBody BusSalesApprovalSalesParam busSalesApprovalSalesParam, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【审批_销售订单】的请求，参数：{},{},{},{},{}", busSalesApprovalSalesParam.getId(), busSalesApprovalSalesParam.getApproval(), currentPrincipal, busSalesApprovalSalesParam.getGoodsName(), busSalesApprovalSalesParam.getGoodsNum());
        iBusSalesService.approvalSales(busSalesApprovalSalesParam.getId(), busSalesApprovalSalesParam.getApproval(), currentPrincipal, busSalesApprovalSalesParam.getGoodsName(), busSalesApprovalSalesParam.getGoodsNum());
        return JsonResult.ok();
    }

    @ApiOperation("分页模糊查询_销售订单")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/busSalers/select')")
    @PostMapping("/select")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求页面", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "idpipo", value = "操作员ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "begin", value = "起始时间", required = false, dataType = "LocalDate"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = false, dataType = "LocalDate"),
            @ApiImplicitParam(name = "approvalStatus", value = "审批状态", required = false, dataType = "Integer")
    })
    public JsonResult page(@RequestBody BusSalesSelectParam busSalesSelectParam) {
        log.debug("开始处理【分页模糊查询_销售订单】的请求，参数：{},{},{}", busSalesSelectParam, busSalesSelectParam.getPage(), busSalesSelectParam.getPageSize());
        PageBean pageBean = iBusSalesService.page(busSalesSelectParam, busSalesSelectParam.getPage(), busSalesSelectParam.getPageSize());
        return JsonResult.ok(pageBean);
    }

    @ApiOperation("销售数据导出")
    @ApiOperationSupport(order = 430)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
            throws IOException {
        String fileName = "销售数据表";
        List<BusSales> list = iBusSalesService.selectAll();
        ExcelUtil.download(response, fileName, BusSales.class, list
        );
    }


    @ApiOperation("下拉选查询_客户名称")
    @ApiOperationSupport(order = 410)
    @PostMapping("/selectCN")
    public JsonResult listCustomerName() {
        log.debug("开始处理【下拉选查询_客户名称】的请求");
        return JsonResult.ok(iBusSalesService.listCustomerName());
    }


    @ApiOperation("下拉选查询_商品名称")
    @ApiOperationSupport(order = 420)
    @PostMapping("/selectGN")
    public JsonResult listGoodsName() {
        log.debug("开始处理【下拉选查询_商品名称】的请求");
        return JsonResult.ok(iBusSalesService.listGoodsName());
    }


}
